import {
    Controller,
    Get,
    Post,
    Put,
    Del,
    Param,
    Body,Inject
  } from '@midwayjs/core';
  import { RoleService } from '../service/role.service';
  import { CreateRoleDto, UpdateRoleDto } from '../dto/Role.dto';
  
  @Controller('/role')
  export class RoleController {
    @Inject()
    roleService: RoleService;
  
    // 获取所有角色
    @Get('/')
    async getAllRoles() {
      const result = await this.roleService.getAllRoles();
      return {
        success: true,
        data: result.data,
        message: result.message,
      };
    }
  
    // 根据 ID 获取角色
    @Get('/:id')
    async getRoleById(@Param('id') id: string) {
      const result = await this.roleService.getRoleById(id);
      if (!result.success) {
        return { success: false, message: result.message, status: 404 };
      }
      return { success: true, data: result.data, message: result.message };
    }
  
    // 创建新角色
    @Post('/')
    async createRole(@Body() body: CreateRoleDto) {
      if (!body.name) {
        return { success: false, message: 'Role name is required', status: 400 };
      }
      const result = await this.roleService.createRole(body);
      if (!result.success) {
        return { success: false, message: result.message, status: 400 };
      }
  
      return {
        success: true,
        data: result.data,
        message: result.message,
      };
    }
  
    // 更新角色
    @Put('/:id')
    async updateRole(@Param('id') id: string, @Body() body: UpdateRoleDto) {
      const result = await this.roleService.updateRole(id, body);
      if (!result.success) {
        return { success: false, message: result.message, status: 404 };
      }
      return { success: true, data: result.data, message: result.message };
    }
  
    // 删除角色
    @Del('/:id')
    async deleteRole(@Param('id') id: string) {
      const result = await this.roleService.deleteRole(id);
      if (!result.success) {
        return { success: false, message: result.message, status: 404 };
      }
      return { success: true, message: result.message };
    }
  }
  